﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Shared
{
    using SharingCenter.Web.Classes;

    public class ContextHandlingEventArgs : EventArgs
    {
        object context;
        public object ContextValue { get { return context; } }

        public ContextHandlingEventArgs(object contextValue)
        {
            context = contextValue;
        }
    }

    public partial class CommentEntry : System.Web.UI.UserControl
    {
        object ContextValue { get; set; }
        bool _allowDelete = false;
        Guid _userId;
        WebProfile _profile;
        DateTime _commentDate;

        public event EventHandler<ContextHandlingEventArgs> Deleting;
        

        public bool AllowDeleteComment {
            get { return _allowDelete; }
            set 
            {
                _allowDelete = value; 

            }
        }

        public CommentEntry()
        {
            this.Load += new EventHandler(CommentEntry_Load);
        }

        void CommentEntry_Load(object sender, EventArgs e)
        {
            
        }

        void lnkDelete_Click(object sender, EventArgs e)
        {
            Deleting(this, new ContextHandlingEventArgs(ContextValue));
        }

        public string Comment 
        {
            get { return ltrComment.Text; }
            set { ltrComment.Text = value; }
        }

        public Guid UserId {
            get
            {
                return _userId;
            }
            set
            {
                _userId = value;
                if (_allowDelete && _userId == WebProfile.Current.UserId)
                {
                    lnkDelete.Visible = true;
                    lnkDelete.Click += new EventHandler(lnkDelete_Click);
                }
                aspnet_Membership m = scDataContext.Instance.aspnet_Memberships.Single(d => d.UserId == value);
                _profile = WebProfile.GetProfile(m.aspnet_User.UserName);

                if (_profile.Avatar.AvatarImageThumbFileName != "")
                {
                    lnkImage.ImageUrl = string.Format("{0}/{1}?{2}", WebConsts.AVATAR_FOLDER, _profile.Avatar.AvatarImageThumbFileName, DateTime.Now.Ticks);
                }
                else
                {
                    lnkImage.ImageUrl = string.Format("{0}?{1}", WebConsts.AVATAR_DEFAULT_IMG_THUMBNAIL_PATH, DateTime.Now.Ticks);
                }

                lnkUserName.Text = _profile.Personal.NickName;
                lnkUserName.NavigateUrl = string.Format("~/Home/?id={0}", value);
                lnkImage.NavigateUrl = lnkUserName.NavigateUrl;
            }
        }

        public DateTime CommentDate
        {
            get { return _commentDate; }
            set
            {
                _commentDate = value;
                lbDate.Text = Utility.FriendlyDateTime(value, TimeDisplayEnum.LiteralHours, true);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
    }
}